package db.dbTables;

import core.objects.BookComment;
import core.objects.ObjectWithID;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class BookCommentTable extends AbstractDBTable {

    public static final String COLUMN_ID = "book_comment_id";
    public static final String COLUMN_BOOK_ID = "book_id";
    public static final String COLUMN_COMMENTER_ID = "commenter_id";
    public static final String COLUMN_RATING = "rating";
    public static final String COLUMN_COMMENT = "comment";
    public static final String COLUMN_TIMESTAMP = "timestamp";

    final public DbColumn id = new DbColumn(COLUMN_ID, Types.INTEGER, 11, false, this);
    final public DbColumn bookId = new DbColumn(COLUMN_BOOK_ID, Types.INTEGER, 11, false, this);
    final public DbColumn commenterId = new DbColumn(COLUMN_COMMENTER_ID, Types.INTEGER, 11, true, this);
    final public DbColumn rating = new DbColumn(COLUMN_RATING, Types.INTEGER, -1, false, this);
    final public DbColumn comment = new DbColumn(COLUMN_COMMENT, Types.VARCHAR, 140, true, this);
    final public DbColumn timestamp = new DbColumn(COLUMN_TIMESTAMP, Types.TIMESTAMP, 140, false, this);

    public BookCommentTable() {
        super("book_comment");
        setIDColumn(id);
    }

    @Override
    public List<DbColumn> getDbColumns() {
        List<DbColumn> list = new ArrayList<DbColumn>();
        list.add(id);
        list.add(bookId);
        list.add(commenterId);
        list.add(comment);
        list.add(rating);
        list.add(timestamp);
        return list;
    }

    @Override
    public ObjectWithID convertSqlTableToBaseObject(ResultSet resultSet) {
        int id = 0, bookId = 0, commenterId = 0, rating = 0;
        String comment = null;
        Timestamp timestamp = null;
        try {
            id = resultSet.getInt(COLUMN_ID);
            bookId = resultSet.getInt(COLUMN_BOOK_ID);
            commenterId = resultSet.getInt(COLUMN_COMMENTER_ID);
            rating = resultSet.getInt(COLUMN_RATING);
            comment = resultSet.getString(COLUMN_COMMENT);
            timestamp = resultSet.getTimestamp(COLUMN_TIMESTAMP);
        } catch (SQLException ex) {
            Logger.getLogger(AuthorTable.class.getName()).log(Level.SEVERE, null, ex);
        }
        return new BookComment(id, bookId, commenterId, comment, rating, timestamp);
    }
}
